import json

import requests
import unittest
import app
from api.login import LoginAPI
from parameterized import parameterized


def build_data():
    file = app.BASE_DIR + "/data/login.json"
    test_data = []
    with open(file, encoding="utf-8") as f:
        json_data = json.load(f)
        for case_data in json_data:
            json_data = case_data.get("json_data")
            status_code = case_data.get("status_code")
            boolean = case_data.get("boolean")
            status = case_data.get("status")
            msg = case_data.get("msg")
            test_data.append((json_data, status_code, boolean, status, msg))
            print(test_data)
    return test_data


class Testlogin(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginAPI()
        self.session = requests.Session()

    def tearDown(self):
        if self.session:
            self.session.close()

    @parameterized.expand(build_data())
    def test01_login_success(self, json_data, status_code, boolean, status, msg):
        response = self.login_api.login(self.session, json_data)
        print(response.json())
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(boolean, response.json().get("success"))
        self.assertEqual(status, response.json().get("code"))
        self.assertIn(msg, response.json().get("message"))
